package com.ls.config.satoken;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import com.ls.constant.SystemConstants;
import com.ls.entity.Customer;
import com.ls.entity.SysUser;
import com.ls.exception.SysException;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;

/**
 * 彭屿自定义 SaToken 常用方法
 * */
@Slf4j
public class SaTokenUtilPy {
    public static @NotNull Customer getCustomer() {
        return getCustomer("APP");
    }
    public static @NotNull SysUser getSysUser() {
        return getSysUser("PC");
    }
    public static Customer getCustomer(String type) {
        SaSession saSession = StpUtil.getTokenSession();
//        StpUtil.login(userId, SystemConstants.PC);
//        StpUtil.getLoginDevice();

        Long customerId = StpUtil.getLoginIdAsLong();
        //WEB 端 和 APP端 缓存 对象类型 不一样
        Customer customer = (Customer) saSession.get(SystemConstants.APP + "_" + customerId.toString());
        if (customer == null&&"APP".equals(type)) {
            throw new SysException("用户不存在，请重新登录！");
        }
        return customer;
    }

    public static SysUser getSysUser(String type) {
        SaSession saSession = StpUtil.getTokenSession();
        Long userId = StpUtil.getLoginIdAsLong();
        //WEB 端 和 APP端 缓存 对象类型 不一样
        SysUser sysUser = (SysUser) saSession.get(SystemConstants.PC + "_" + userId.toString());
        if (sysUser == null&&"PC".equals(type)) {
            throw new SysException("用户不存在，请重新登录！");
        }
        return sysUser;
    }

}
